﻿using System;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using MediaPortal.Profile;
using MediaPortal.Configuration;

namespace GeckoBrowser.GUIConfiguration
{
    public partial class ConfigForm : Form
    {
        public ConfigForm()
        {
            InitializeComponent();
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
            {
                txtStartpage.Text = settings.GetValueAsString("GeckoBrowser", "Startpage", "");
                txtZoomStep.Value = Convert.ToDecimal(settings.GetValueAsInt("GeckoBrowser", "Zoomstep", 10));
                txtDefaultzoom.Value = Convert.ToDecimal(settings.GetValueAsInt("GeckoBrowser", "Defaultzoom", 100));
                cbMouselessBrowsing.Checked = settings.GetValueAsBool("GeckoBrowser", "RemoteBrowsing", false);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateStartpage())
            {
                using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
                {
                    settings.SetValue("GeckoBrowser", "Startpage", txtStartpage.Text);
                    settings.SetValue("GeckoBrowser", "Zoomstep", txtZoomStep.Value.ToString());
                    settings.SetValue("GeckoBrowser", "Defaultzoom", txtDefaultzoom.Value.ToString());
                    settings.SetValueAsBool("GeckoBrowser", "RemoteBrowsing", cbMouselessBrowsing.Checked);
                }
                Close();
            }
            else
            {
                MessageBox.Show("The url (" + txtStartpage.Text + ") you've entered is not a valid url. Please enter a valid url as startpage!", "Not a valid URL", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
        public static bool IsValidUrl(String Url)
        {
            return Regex.IsMatch(Url, @"((https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)");
        }




        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnShowAdvancedConfig_Click(object sender, EventArgs e)
        {
            AdvancedConfigForm acf = new AdvancedConfigForm();
            acf.ShowDialog();
        }

        private void txtStartpage_Validating(object sender, CancelEventArgs e)
        {
            ValidateStartpage();
        }
        private bool ValidateStartpage()
        {
            if (txtStartpage.Text != string.Empty)
            {
                System.Globalization.CompareInfo cmpUrl = System.Globalization.CultureInfo.InvariantCulture.CompareInfo;
                if (cmpUrl.IsPrefix(txtStartpage.Text, "http://") == false && cmpUrl.IsPrefix(txtStartpage.Text, "https://") == false)
                {
                    txtStartpage.Text = "http://" + txtStartpage.Text;
                }
                if (!IsValidUrl(txtStartpage.Text))
                {
                    errorProvider.SetError(txtStartpage, "This is not a valid URL. Please check your entered URL!");
                    return false;
                }
            }
            errorProvider.SetError(txtStartpage, string.Empty);
            return true;
        }
    }
}
